package com.lngsyz.eseal.base.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@ComponentScan("com.lngsyz.ywtb")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() { // 2
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(30); // 核心线程数
        taskExecutor.setMaxPoolSize(30); // 最大线程数
        taskExecutor.setQueueCapacity(30); // 最大等待队列数
        //taskExecutor.setKeepAliveSeconds(60);//除核心线程，其他线程保留时间
        //taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//队列满之后，且创建的线程数大于最大线程数之后，直接使用调用任务的线程执行
        //taskExecutor.setThreadNamePrefix("taskExecutor--");
        //taskExecutor.setWaitForTasksToCompleteOnShutdown(true);//线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
        //taskExecutor.setAwaitTerminationSeconds(60);//设置线程池中任务的等待时间，超过时间强制销毁。
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}
